//
//  XMTabBarViewController.m
//  XMKit
//
//  Created by chenming on 2018/5/26.
//  Copyright © 2018年 ChenXiaoMing. All rights reserved.
//

#import "XMTabBarViewController.h"

// Controller
#import "XMCategoryViewController.h"
#import "XMModulersViewController.h"
#import "XMNavigationViewController.h"

@interface XMTabBarViewController ()

@end

@implementation XMTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 模块
    [self addViewControllerClassName:[XMModulersViewController description] imageName:@"" titles:@"模块"];
    
    // 分类
    [self addViewControllerClassName:[XMCategoryViewController description] imageName:@"" titles:@"分类"];
}

- (BOOL)shouldAutorotate {
    return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

+ (void)initialize {
    // 设置为不透明
    [[UITabBar appearance] setTranslucent:NO];
    
    // 设置背景颜色
    [UITabBar appearance].barTintColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    
    // 拿到整个导航控制器的外观
    UITabBarItem *item = [UITabBarItem appearance];
    
    // 普通状态
    NSMutableDictionary *normalAtts = [NSMutableDictionary dictionary];
    normalAtts[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    normalAtts[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"999999" alpha:1];
    [item setTitleTextAttributes:normalAtts forState:UIControlStateNormal];
    
    // 选中状态
    NSMutableDictionary *selectAtts = [NSMutableDictionary dictionary];
    selectAtts[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectAtts[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"00bfff" alpha:1];
    [item setTitleTextAttributes:selectAtts forState:UIControlStateSelected];
    
}

// 设置子控制器的图片和文字
- (void)addViewControllerClassName:(NSString *)className
                         imageName:(NSString *)imageName
                            titles:(NSString *)titles {
    UIViewController *viewController = [[NSClassFromString(className) alloc]init];
    
    //系统返回手势
    XMNavigationViewController *xmNavigation = [[XMNavigationViewController alloc] initWithRootViewController:viewController];
    
    xmNavigation.tabBarItem.title = titles;
//    xmNavigation.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    xmNavigation.tabBarItem.imageInsets = UIEdgeInsetsMake(5, 0, -5, 0);

//删除字符串
//    stwNavigation.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByReplacingOccurrencesOfString:@"_normal" withString:@""]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

//拼接字符串
//    stwNavigation.tabBarItem.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"high"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //添加子视图
    [self addChildViewController:xmNavigation];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
